package com.beemans.admob.ads;

import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;

import com.google.android.gms.ads.MobileAds;

public final class AbAdvertManager {

    public static void init(@NonNull Context context) {
        new Thread(() -> MobileAds.initialize(context)).start();
    }


    // =================================== 信息流广告 ===================================

    /**
     * @param nativeAdvertConfig 广告配置
     * @return 信息流广告是否已准备完毕
     */
    public static boolean isNativeAdvertReady(
            @NonNull AbNativeAdvertConfig nativeAdvertConfig
    ) {
        final String placementId = nativeAdvertConfig.getPlacementId();
        return AbAdvertLoadManager.getInstance().isNativeAdvertReady(placementId);
    }

    /**
     * @param nativeAdvertConfig 广告配置
     * @return 信息流广告是否正在加载
     */
    public static boolean isNativeAdvertLoading(
            @NonNull AbNativeAdvertConfig nativeAdvertConfig
    ) {
        final String placementId = nativeAdvertConfig.getPlacementId();
        return AbAdvertLoadManager.getInstance().isNativeAdvertLoading(placementId);
    }

    /**
     * 加载信息流广告
     *
     * @param lifecycleOwner       传入 FragmentActivity 或 Fragment
     * @param nativeAdvertConfig   广告配置
     * @param nativeSelfRender     自渲染视图
     * @param nativeAdvertCallback 广告回调
     */
    public static void loadNativeAdvert(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull AbNativeAdvertConfig nativeAdvertConfig,
            @Nullable IAbNativeSelfRender nativeSelfRender,
            @Nullable AbNativeAdvertCallback nativeAdvertCallback
    ) {
        final AbNativeAdvert nativeAdvert = new AbNativeAdvert(
                lifecycleOwner,
                nativeAdvertConfig,
                nativeSelfRender,
                nativeAdvertCallback
        );
        nativeAdvert.loadAdvert();
    }

    // =================================== 信息流广告 ===================================


    // =================================== 横幅广告 ===================================

    /**
     * 加载横幅广告
     *
     * @param lifecycleOwner       传入 FragmentActivity 或 Fragment
     * @param viewGroup            广告容器
     * @param bannerAdvertConfig   广告配置
     * @param bannerAdvertCallback 广告回调
     */
    public static void loadBannerAdvert(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull ViewGroup viewGroup,
            @NonNull AbBannerAdvertConfig bannerAdvertConfig,
            @Nullable AbBannerAdvertCallback bannerAdvertCallback
    ) {
        final AbBannerAdvert bannerAdvert = new AbBannerAdvert(
                lifecycleOwner,
                viewGroup,
                bannerAdvertConfig,
                bannerAdvertCallback
        );
        bannerAdvert.loadAdvert();
    }

    // =================================== 横幅广告 ===================================


    // =================================== 开屏广告 ===================================

    /**
     * @param lifecycleOwner     传入 FragmentActivity 或 Fragment
     * @param splashAdvertConfig 广告配置
     * @return 开屏广告是否已准备完毕
     */
    public static boolean isSplashAdvertReady(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull AbSplashAdvertConfig splashAdvertConfig
    ) {
        final AbSplashAdvert splashAdvert = new AbSplashAdvert(lifecycleOwner, splashAdvertConfig);
        return splashAdvert.isAdvertReady();
    }

    /**
     * @param lifecycleOwner     传入 FragmentActivity 或 Fragment
     * @param splashAdvertConfig 广告配置
     * @return 开屏广告是否正在加载
     */
    public static boolean isSplashAdvertLoading(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull AbSplashAdvertConfig splashAdvertConfig
    ) {
        final AbSplashAdvert splashAdvert = new AbSplashAdvert(lifecycleOwner, splashAdvertConfig);
        return splashAdvert.isAdvertLoading();
    }

    /**
     * 加载开屏广告
     *
     * @param lifecycleOwner       传入 FragmentActivity 或 Fragment
     * @param splashAdvertConfig   广告配置
     * @param splashAdvertCallback 广告回调
     */
    public static void loadSplashAdvert(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull AbSplashAdvertConfig splashAdvertConfig,
            @Nullable AbSplashAdvertCallback splashAdvertCallback
    ) {
        final AbSplashAdvert splashAdvert = new AbSplashAdvert(
                lifecycleOwner,
                splashAdvertConfig,
                splashAdvertCallback
        );
        splashAdvert.loadAdvert();
    }

    // =================================== 开屏广告 ===================================


    // =================================== 插屏广告 ===================================

    /**
     * @param lifecycleOwner     传入 FragmentActivity 或 Fragment
     * @param insertAdvertConfig 广告配置
     * @return 插屏广告是否已准备完毕
     */
    public static boolean isInertAdvertReady(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull AbInsertAdvertConfig insertAdvertConfig
    ) {
        final AbInsertAdvert insertAdvert = new AbInsertAdvert(lifecycleOwner, insertAdvertConfig);
        return insertAdvert.isAdvertReady();
    }

    /**
     * @param lifecycleOwner     传入 FragmentActivity 或 Fragment
     * @param insertAdvertConfig 广告配置
     * @return 插屏广告是否正在加载
     */
    public static boolean isInertAdvertLoading(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull AbInsertAdvertConfig insertAdvertConfig
    ) {
        final AbInsertAdvert insertAdvert = new AbInsertAdvert(lifecycleOwner, insertAdvertConfig);
        return insertAdvert.isAdvertLoading();
    }

    /**
     * 加载插屏广告
     *
     * @param lifecycleOwner       传入 FragmentActivity 或 Fragment
     * @param insertAdvertConfig   广告配置
     * @param insertAdvertCallback 广告回调
     */
    public static void loadInsertAdvert(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull AbInsertAdvertConfig insertAdvertConfig,
            @Nullable AbInsertAdvertCallback insertAdvertCallback
    ) {
        final AbInsertAdvert insertAdvert = new AbInsertAdvert(
                lifecycleOwner,
                insertAdvertConfig,
                insertAdvertCallback
        );
        insertAdvert.loadAdvert();
    }

    // =================================== 插屏广告 ===================================


    // =================================== 激励广告 ===================================

    /**
     * @param lifecycleOwner     传入 FragmentActivity 或 Fragment
     * @param rewardAdvertConfig 广告配置
     * @return 激励广告是否已准备完毕
     */
    public static boolean isRewardAdvertReady(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull AbRewardAdvertConfig rewardAdvertConfig
    ) {
        final AbRewardAdvert rewardAdvert = new AbRewardAdvert(lifecycleOwner, rewardAdvertConfig);
        return rewardAdvert.isAdvertReady();
    }

    /**
     * @param lifecycleOwner     传入 FragmentActivity 或 Fragment
     * @param rewardAdvertConfig 广告配置
     * @return 激励广告是否正在加载
     */
    public static boolean isRewardAdvertLoading(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull AbRewardAdvertConfig rewardAdvertConfig
    ) {
        final AbRewardAdvert rewardAdvert = new AbRewardAdvert(lifecycleOwner, rewardAdvertConfig);
        return rewardAdvert.isAdvertLoading();
    }

    /**
     * 加载激励广告
     *
     * @param lifecycleOwner       传入 FragmentActivity 或 Fragment
     * @param rewardAdvertConfig   广告配置
     * @param rewardAdvertCallback 广告回调
     */
    public static void loadRewardAdvert(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull AbRewardAdvertConfig rewardAdvertConfig,
            @Nullable AbRewardAdvertCallback rewardAdvertCallback
    ) {
        final AbRewardAdvert rewardAdvert = new AbRewardAdvert(
                lifecycleOwner,
                rewardAdvertConfig,
                rewardAdvertCallback
        );
        rewardAdvert.loadAdvert();
    }

    // =================================== 激励广告 ===================================
}